// action ->  mutation -> state
import * as types from '../types'
import api from '../../../api'

const state = {
    loginIn: false,
    account:{
        "openid":"",
        "nickname":"",
        "sex":1,
        "language":"",
        "city":"",
        "province":"",
        "country":"",
        "headimgurl": "",
        "subscribe_time":0,
    }
}

const mutations = {
    [types.SET_ACCOUNT](state, account){
        state.account = account;
        state.loginIn = true;
    }
}

const actions = {
    getAccount({commit, state}, { callback_url, scope, cbk, key }){
        
        if( !state.loginIn ){
            api.authorize.check({ key }).then(({dispose})=>{
                if( dispose == 1){
                    if( key == 1){
                        cbk&&cbk();
                    }else{
                        api.simpleOrder.staffInfo().then((data)=>{
                            commit(types.SET_ACCOUNT, data);
                            cbk&&cbk();
                        });
                    }
                }else{
                    api.authorize.go(callback_url, scope||'snsapi_userinfo');
                }
            });
        }
    }
}

export default {
    state,
    mutations,
    actions
}